package com.qiyun.framework.helper;

import android.content.Context;

import org.xutils.DbManager;
import org.xutils.x;

/**
 * 数据库创建类
 *
 * @author chandler
 * @date 2015/12/10
 */
public class DBHelper {

    //数据库名称
    private static final String DB_NAME = "smartlogistics";
    //数据库版本
    private static final int DB_VERSION = 1;
    private static DbManager dbUtils;
    private static Context mContext;

    public static void init(Context context){
        mContext=context;
    }

    //配置db
    private static DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
            .setDbName(DB_NAME+".db")
            // 不设置dbDir时, 默认存储在app的私有目录.
            .setDbVersion(1)
            .setDbOpenListener(new DbManager.DbOpenListener() {
                @Override
                public void onDbOpened(DbManager db) {
                    // 开启WAL, 对写入加速提升巨大
                    db.getDatabase().enableWriteAheadLogging();
                }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbManager db, int oldVersion, int newVersion) {

                }
   });


    public static DbManager getInstance(){
        if(mContext==null){
            throw new IllegalStateException("please call init before call me");
        }else{
            if(dbUtils==null) {
                DbManager db = x.getDb(daoConfig);
            }
            return dbUtils;
        }
    }
}
